package com.adcc.ags.gms.core.util;

import com.adcc.utility.log.Log;
import com.adcc.utility.mq.configuration.MQConfiguration;
import com.adcc.utility.mq.configuration.ibm.IBMMQConfiguration;
import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.Hashtable;

/**
 * 应用配置类
 */
@Configuration
@PropertySource({"classpath:conf.properties"})
@EnableWebMvc
public class AppConfiguration extends WebMvcConfigurerAdapter{

    @Autowired
    private Environment env;

    public String getGW1ServiceURL(){
        if(env.containsProperty("GW1.Service.URL")){
            return env.getProperty("GW1.Service.URL");
        }else{
            return Strings.nullToEmpty("");
        }
    }

    public String getGW1MonitorURL(){
        if(env.containsProperty("GW1.Monitor.URL")){
            return env.getProperty("GW1.Monitor.URL");
        }else{
            return Strings.nullToEmpty("");
        }
    }

    /**
     * 取得SourceMQIP
     * @return
     */
    public String getSourceMQIP(){
        if(env.containsProperty("SourceMQ.IP")){
            return env.getProperty("SourceMQ.IP");
        }else{
            return Strings.nullToEmpty("");
        }
    }

    /**
     * 取得SourceMQPort
     * @return
     */
    public int getSourceMQPort(){
        if(env.containsProperty("SourceMQ.Port")){
            try{
                return Integer.parseInt(env.getProperty("SourceMQ.Port"));
            }catch (Exception ex){
                Log.error("getSourceMQPort() error",ex);
                return 1414;
            }
        }else {
            return 1414;
        }
    }

    /**
     * 取得SourceMQQM
     * @return
     */
    public String getSourceMQQM(){
        if(env.containsProperty("SourceMQ.QM")){
            return env.getProperty("SourceMQ.QM");
        }else{
            return Strings.nullToEmpty("");
        }
    }

    /**
     * 取得SourceMQChannel
     * @return
     */
    public String getSourceMQChannel(){
        if(env.containsProperty("SourceMQ.Channel")){
            return env.getProperty("SourceMQ.Channel");
        }else{
            return Strings.nullToEmpty("");
        }
    }

    /**
     * 取得SourceMQActiveMode
     * @return
     */
    public int getSourceMQActiveMode(){
        if(env.containsProperty("SourceMQ.ActiveMode")){
            try{
                return Integer.parseInt(env.getProperty("SourceMQ.ActiveMode"));
            }catch (Exception ex){
                Log.error("getSourceMQActiveMode() error",ex);
                return 1;
            }
        }else{
            return 1;
        }
    }

    /**
     * 取得SourceMQTimeout
     * @return
     */
    public long getSourceMQTimeout(){
        if(env.containsProperty("SourceMQ.Timeout")){
            try{
                return Long.parseLong(env.getProperty("SourceMQ.Timeout"));
            }catch (Exception ex){
                Log.error("getSourceMQTimeout() error",ex);
                return 3000;
            }
        }else {
            return 3000;
        }
    }

    /**
     * 取得SourceMQMaxConnections
     * @return
     */
    public int getSourceMQMaxConnections(){
        if(env.containsProperty("SourceMQ.MaxConnections")){
            try{
                return Integer.parseInt(env.getProperty("SourceMQ.MaxConnections"));
            }catch (Exception ex){
                Log.error("getSourceMQMaxConnections() error",ex);
                return 1024;
            }
        }else {
            return 1024;
        }
    }

    /**
     * 取得SourceMQMaxIdelConnections
     * @return
     */
    public int getSourceMQMaxIdelConnections(){
        if(env.containsProperty("SourceMQ.MaxIdelConnections")){
            try{
                return Integer.parseInt(env.getProperty("SourceMQ.MaxIdelConnections"));
            }catch (Exception ex){
                Log.error("getSourceMQMaxIdelConnections() error",ex);
                return 500;
            }
        }else {
            return 500;
        }
    }

    /**
     * 取得TargetMQIP
     * @return
     */
    public String getTargetMQIP(){
        if(env.containsProperty("TargetMQ.IP")){
            return env.getProperty("TargetMQ.IP");
        }else{
            return Strings.nullToEmpty("");
        }
    }

    /**
     * 取得TargetMQPort
     * @return
     */
    public int getTargetMQPort(){
        if(env.containsProperty("TargetMQ.Port")){
            try{
                return Integer.parseInt(env.getProperty("TargetMQ.Port"));
            }catch (Exception ex){
                Log.error("get TargetMQ.Port error");
                return 1414;
            }
        }else {
            return 1414;
        }
    }

    /**
     * 取得TargetMQQM
     * @return
     */
    public String getTargetMQQM(){
        if(env.containsProperty("TargetMQ.QM")){
            return env.getProperty("TargetMQ.QM");
        }else{
            return Strings.nullToEmpty("");
        }
    }

    /**
     * 取得TargetMQChannel
     * @return
     */
    public String getTargetMQChannel(){
        if(env.containsProperty("TargetMQ.Channel")){
            return env.getProperty("TargetMQ.Channel");
        }else{
            return Strings.nullToEmpty("");
        }
    }

    /**
     * 取得TargetMQActiveMode
     * @return
     */
    public int getTargetMQActiveMode(){
        if(env.containsProperty("TargetMQ.ActiveMode")){
            try{

            }catch (Exception ex){
                Log.error("getTargetMQActiveMode() error",ex);
                return 1;
            }
            return Integer.parseInt(env.getProperty("TargetMQ.ActiveMode"));
        }else{
            return 1;
        }
    }

    /**
     * 取得TargetMQTimeout
     * @return
     */
    public long getTargetMQTimeout(){
        if(env.containsProperty("TargetMQ.Timeout")){
            try{
                return Long.parseLong(env.getProperty("TargetMQ.Timeout"));
            }catch (Exception ex){
                Log.error("getTargetMQTimeout() error",ex);
                return 3000;
            }
        }else {
            return 3000;
        }
    }

    /**
     * 取得TargetMQMaxConnections
     * @return
     */
    public int getTargetMQMaxConnections(){
        if(env.containsProperty("TargetMQ.MaxConnections")){
            try{
                return Integer.parseInt(env.getProperty("TargetMQ.MaxConnections"));
            }catch (Exception ex){
                Log.error("getTargetMQMaxConnections() error",ex);
                return 1024;
            }
        }else {
            return 1024;
        }
    }

    /**
     * 取得TargetMQMaxIdelConnections
     * @return
     */
    public int getTargetMQMaxIdelConnections(){
        if(env.containsProperty("TargetMQ.MaxIdelConnections")){
            try{
                return Integer.parseInt(env.getProperty("TargetMQ.MaxIdelConnections"));
            }catch (Exception ex){
                Log.error("getTargetMQMaxIdelConnections() error",ex);
                return 500;
            }
        }else {
            return 500;
        }
    }

    /**
     * 取得GWUserFilePath
     * @return
     */
    public String getGWUserFilePath(){
        return env.getProperty("GWUser.FilePath");
    }

    /**
     * 取得SysConfig.qmConnNumber
     * @return
     */
    public String getQmConnNumber(){
        if(env.containsProperty("SysConfig.qmConnNumber")){
            return env.getProperty("SysConfig.qmConnNumber");
        }else{
            return "1000";
        }
    }

    /**
     * 取得SysConfig.queueDepthThreshold
     * @return
     */
    public String getQueueDepthThreshold(){
        if(env.containsProperty("SysConfig.queueDepthThreshold")){
            return env.getProperty("SysConfig.queueDepthThreshold");
        }else{
            return "1000";
        }
    }

    @Bean
    public MQFactory mqFactory() throws Exception {
        MQConfiguration sourceConfiguration = new IBMMQConfiguration(getSourceMQIP(),getSourceMQPort(),getSourceMQQM(),getSourceMQChannel());
        Hashtable<String,Object> params1 = new Hashtable<String,Object>();
        params1.put("ActiveMode",getSourceMQActiveMode());
        params1.put("Timeout",getSourceMQTimeout());
        params1.put("MaxConnections",getSourceMQMaxConnections());
        params1.put("MaxIdelConnections",getSourceMQMaxIdelConnections());
        MQConfiguration targetConfiguration = new IBMMQConfiguration(getTargetMQIP(),getTargetMQPort(),getTargetMQQM(),getTargetMQChannel());
        Hashtable<String,Object> params2 = new Hashtable<String,Object>();
        params2.put("ActiveMode",getSourceMQActiveMode());
        params2.put("Timeout",getSourceMQTimeout());
        params2.put("MaxConnections",getSourceMQMaxConnections());
        params2.put("MaxIdelConnections",getSourceMQMaxIdelConnections());
        return new MQFactory(sourceConfiguration,params1,targetConfiguration,params2);
    }

    @Bean
    public LogFactory logFactory() throws Exception{
        return new LogFactory();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
